/**
 * @author : zzr
 * @description :
 * @date :
 */
public class Test2 {
    public static MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA, MySingleList.ListNode headB) {

        //1.分别求2个链表的长度
        int lenA = 0;
        int lenB = 0;

        MySingleList.ListNode pL = headA; //假设pL所指向的链表是最长的链表
        MySingleList.ListNode pS = headB; //假设pS所指向的链表是最短的链表
        while(pL != null) {
            lenA++;
            pL = pL.next;
        }
        while(pS != null) {
            lenB++;
            pS = pS.next;
        }

        //如果没有下面的两行代码，pL和pS指向的都为null了
        pL = headA;
        pS = headB;

        //分别求得了两个链表的长度
        int len = lenA - lenB;
        //但无法确定到底那个链表的长度更长，可以在这里加一个if条件进行修正指向和len的差值
        if(len < 0) {
            pL = headB;
            pS = headA;
            len = lenB - lenA;
        }

        //这样就能保证pL一定指向最长的链表， pS一定指向最短的链表 len一定是一个正数

        //让最长的链表 先走差值步
        while(len != 0) {
            pL = pL.next;
            len--;
        }

        //相遇点
        while(pL != pS) {
            pL = pL.next;
            pS = pS.next;
        }
        return pS;
    }

    public static void createIntersect(MySingleList.ListNode headA,
                                       MySingleList.ListNode headB) {
        headB.next.next = headA.next.next;
    }

    public static void main(String[] args) {
        MySingleList mySingleList3  = new MySingleList();
        mySingleList3.addLast(12);
        mySingleList3.addLast(23);
        mySingleList3.addLast(34);
        mySingleList3.addLast(45);
        mySingleList3.addLast(56);

        MySingleList mySingleList4 = new MySingleList();
        mySingleList4.addLast(8);
        mySingleList4.addLast(15);
        mySingleList4.addLast(40);
        mySingleList4.addLast(50);

        createIntersect(mySingleList3.head, mySingleList4.head); //34

        MySingleList.ListNode ret = getIntersectionNode(mySingleList3.head, mySingleList4.head);

        System.out.println(ret.val);

    }
}
